#!/bin/sh
#
# Run a couple of tests with bconsole
#
TestName="bconsole-test"
JobName=backup
. scripts/functions

scripts/cleanup
scripts/copy-test-confs

#
# Zap out any schedule in default conf file so that
#  it doesn't start during our test
#
outf="$tmp/sed_tmp"
echo "s%  Schedule =%# Schedule =%g" >${outf}
cp $conf/susan-dir.conf $tmp/1
sed -f ${outf} $tmp/1 >$conf/susan-dir.conf

change_jobname BackupClient1 $JobName
start_test

# start the director
touch $tmp/bconcmds
run_susan

print_debug "Test bconsole -l and -D with a single director definition"

$bin/bconsole -c $conf/bconsole.conf -l > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "Bconsole doesn't support -l option"
    estat=1
fi

nb=`cat $tmp/1 | wc -l`
if [ "$nb" -ne 1 ]; then
    print_debug "Found more than one director in bconsole -l output"
    estat=2
fi

grep "$HOST-dir" $tmp/1 > /dev/null
if [ $? -ne 0 ]; then
    print_debug "bconsole -l option doesn't display $HOST-dir"
    estat=3
fi

echo quit | $bin/bconsole -c $conf/bconsole.conf -D "this is a test" > $tmp/1
if [ $? -eq 0 ]; then
    print_debug "Bconsole should abort if selected directors isn't defined"
    estat=4
fi

echo quit | $bin/bconsole -c $conf/bconsole.conf -D "$HOST-dir" > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "bconsole doesn't connect to $HOST-dir"
    estat=5
fi

perl -Mscripts::functions \
    -e "extract_resource('$conf/bconsole.conf', 'Director', '$HOST-dir')" \
    > $tmp/1

sed "s/$HOST-dir/bconsole-test-dir/" $tmp/1 >> $conf/bconsole.conf

$bin/bconsole -t -c $conf/bconsole.conf > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "configuration tweak failed"
    estat=6
fi

$bin/bconsole -c $conf/bconsole.conf -l > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "Bconsole doesn't support -l option"
    estat=7
fi

nb=`cat $tmp/1 | wc -l`
if [ "$nb" -ne 2 ]; then
    print_debug "Found more than one director in bconsole -l output"
    estat=8
fi

grep "$HOST-dir" $tmp/1 > /dev/null
if [ $? -ne 0 ]; then
    print_debug "bconsole -l option doesn't display $HOST-dir"
    estat=9
fi

grep "bconsole-test-dir" $tmp/1 > /dev/null
if [ $? -ne 0 ]; then
    print_debug "bconsole -l option doesn't display bconsole-test-dir"
    estat=10
fi

echo quit | $bin/bconsole -c $conf/bconsole.conf -D "this is a test" > $tmp/1
if [ $? -eq 0 ]; then
    print_debug "Bconsole should abort if selected directors isn't defined"
    estat=11
fi

echo quit | $bin/bconsole -c $conf/bconsole.conf -D "$HOST-dir" > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "bconsole doesn't connect to $HOST-dir"
    estat=12
fi

echo quit | $bin/bconsole -c $conf/bconsole.conf -D "bconsole-test-dir" > $tmp/1
if [ $? -ne 0 ]; then
    print_debug "bconsole doesn't connect to bconsole-test-dir"
    estat=13
fi


stop_susan
end_test
